﻿<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>MemberSale</title>
    <script src="../../Scripts/jquery-1.9.1.js" type="text/javascript"></script>
    <script src="../../Content/JS/layer-v1.7.1/layer/layer.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.fs.stepper.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.fs.stepper1.js" type="text/javascript"></script>
    <link href="../../Content/Themes/blue2/css.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../../CSS/NumberUpDown.css" />
    <style type="text/css">  
        .allContent{ width:600px; margin:0px auto;}
        .fieldCss{border: solid 1px #C6E2FF; width: 550px; padding: 10px 10px 10px 10px;}
        .field_goods{border: solid 1px #C6E2FF; width: 240px; padding: 10px 10px 10px 10px;}
        .filed_Div{float:left;  margin-left:10px;}
        .field_DH_goods{border: solid 1px #C6E2FF; width: 200px; padding: 10px 10px 10px 10px;float:left;}
        #WDH_goods,#DH_goods{border-color:#1C1C1C;  width:200px;overflow:scroll;text-align:center; border-collapse:collapse; border-spacing:0;}
        #WDH_goods td{word-break:keep-all; white-space:nowrap; padding:3px;border:1px black solid ;}
        #DH_goods td{word-break:keep-all; white-space:nowrap; padding:3px;border:1px black solid ;}
        #showgoods{border-color:#1C1C1C;  width:400px;overflow:scroll;text-align:center; border-collapse:collapse; border-spacing:0; margin-left:10px; }
        #showgoods td{word-break:keep-all; white-space:nowrap; padding:3px;border:1px black solid ;}
    a:link,a:visited{font-size:12px;color:#666;text-decoration:none;}
	a:hover{color:#ff0000;text-decoration:underline; background:#6a9fe2;}
	#Tab{width:220px;}
	.Menubox{height:28px;border-bottom:1px solid #C6E2FF;width:572px;}
	.Menubox ul{list-style:none;margin:7px;padding:0;position:absolute;}
	.Menubox ul li{float:left;background:#619FE0;line-height:20px;display:block;cursor:pointer;width:65px;text-align:center;color:#fff;font-weight:bold;border-top:1px solid #C6E2FF;border-left:1px solid #C6E2FF;border-right:1px solid #C6E2FF;}
	.Menubox ul li.hover{background:#fff;border-bottom:1px solid #fff;color:#4169E1;}
	.Contentbox{clear:both;margin-top:0px;border-top:none;height:100%;padding-top:8px; width:570px; border-bottom:1px solid #C6E2FF; border-right:1px solid #C6E2FF; border-left:1px solid #C6E2FF; overflow:scroll;}
	.Contentbox ul{list-style:none;margin:7px;padding:0;}
	.Contentbox ul li{line-height:24px;border-bottom:1px dotted #ccc;}
    </style>
</head>
<body class="main">
    <div class="AllWebTopDiv">
        <div class="path">
            <div class="nav">
                <a href="#">会员消费</a>
            </div>
        </div>
        <div class="content"> 
          <div class="allContent">
            <fieldset class="fieldCss">
                <legend>会员信息</legend>
                <form>
                <label>
                    会员编号：</label><%:Html.TextBox("memberId", null, new { @readonly = "readonly",@class="Line"  })%>
                <label>
                    当前积分：</label><%:Html.TextBox("point", null, new { @readonly = "readonly",@class="Line" })%><br />
                <br />
                <label>
                    会员姓名：</label><%:Html.TextBox("memberName", null, new { @readonly = "readonly", @class = "Line" })%>
                <label>
                    当前余额：</label><%:Html.TextBox("balance", null, new { @readonly = "readonly", @class = "Line" })%>
                <br />
                <br />
                <label>
                    交易密码：</label><%:Html.Password("pwd", null, new { @class = "Line" })%>
                <label>
                    特殊折扣：</label>
                <input type="number" id="discount" value="10" step="0.1"  min="0" max="10" readonly ="readonly" onpropertychange="Afterdt()"/><%--<label 
                    style="margin-left: 45px; margin-top:7px; position:absolute; font-weight:bold; left: 312px;">折</label>
                <input type="button" id="Eable" value="开启" onclick="tsDiscount()" class="button2" style="margin-left: 70px;" />
                &nbsp;&nbsp;&nbsp;<input type="button" id="yiJia" value="开启议价" onclick="Afterdt()" class="button2"  style="margin-left: 70px;" />--%>
                </form>
            </fieldset>
            <fieldset class="fieldCss">
                <legend>会员信息</legend>
                <form>
                &nbsp;
                <label>
                    本次消费：</label><%:Html.TextBox("sale", 0, new {@readonly = "readonly", @class = "Line", onpropertychange="changeCardPay()"})%>
                &nbsp;<label>积分支付：</label><%:Html.TextBox("pointPay", null, new { @class = "Line", @readonly = "readonly" })%>
                <br />
                <br />
                &nbsp;<label>
                    卡片支付：</label><%:Html.TextBox("cardPay", 0, new { @readonly = "readonly", @class = "Line" })%>
                &nbsp;<label>现金支付：</label><%:Html.TextBox("carhPay", 0, new { @readonly = "readonly", @class = "Line" })%>
                <br />
                <br />
                <label>
                    折扣后金额：</label><%:Html.TextBox("discountAmount", 0, new { @readonly = "readonly", @class = "Line"})%>
                <label>
                    交易后积分：</label><%:Html.TextBox("jifen", null, new { @readonly = "readonly", @class = "Line" })%>
              
                </form>
            </fieldset>
            <div id="Tab">
                <div class="Menubox">
                    <ul>
                    
                       <!-- <li id="menu1" onmouseover="setTab('menu',1,2)" >套餐</li>-->
                        <li id="menu2" onmouseover="setTab('menu',2,2)" class="hover">商品</li>
                    </ul>
                </div>
                <div class="Contentbox">
                    <div id="con_menu_1"  style="display:none;">
                     <div class="filed_Div">
                     <fieldset class="field_goods">
                       <legend>未兑换的商品</legend>
                        <table id="WDH_goods">
                        </table>
                        </fieldset>
                      </div>
                      <div class="filed_Div">
                        <fieldset class="field_goods">
                         <legend>要兑换的商品</legend>
                          <table id="DH_goods">
                          <tr class="DGHeaderStyle"><td>商品名称</td><td>数量</td><td>单位</td></tr>
                        </table>
                        </fieldset>
                     </div>
                    </div>
                    <div id="con_menu_2"  class="hover">
                      <div style=" margin-left:10px;">
                        <label>
                            商品：</label>
                        <select id="sgoods" >
                          
                           <% for (int i = 0; i < Model.Count; i++)
                              {%>
                              <option value="<%:Model[i].id %>" ><%:Model[i].goodsName%></option>

                           <%} %>
                        </select>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <label>
                            数量：</label>
                            <input type="number" id="num" value="1" step="1" min="1"  readonly="readonly"/>
                     
                        <input  type="button" value="添加" onclick="addGoods()" class="button2" style="margin-left:55px;"/>
                    </div>
                         <table id="showgoods">
                         <tr class="DGHeaderStyle">
                          <td>商品</td>
                          <td>价格</td>
                          <td>数量</td>
                          <td>操作</td>
                        </tr>
                        </table>
                    </div>
                </div>
            </div>
            <div>
             <fieldset class="fieldCss">
                <legend>票据打印</legend>
                <form>
                <input type="checkbox" />
                <label>票据打印</label>
                 &nbsp;<label>
                    附加信息：</label><%:Html.TextBox("Msg", null, new { @readonly = "readonly", @class = "Line" })%>
                    &nbsp;<label>
                    接待人：</label><%:Html.TextBox("jiedairen", null, new { @readonly = "readonly", @class = "Line" })%>
                <br />
                <br />
                </form>
            </fieldset>
              <br />
                
                <input  type="button" value="结算" onclick="MemberAccount()" class="button2" style=" margin-left:120px;"/>
                <input  type="button" value="退出" onclick="closeWindows()" class="button2" style=" margin-left:120px;"/>
            </div>
              <p>
               <object classid="clsid:2323E9E1-2B2E-4BD2-9F66-1C2D02F7A29C" codebase="vcp400.cab" width="0" height="0" id="vcp400">
               </object>
              </p>
          </div>
        </div>
    </div>
</body>
</html>
<script type="text/javascript">
    $(document).ready(function () {
        $("#num").stepper();
        $("#discount").stepper1();
    });
    
     var  x=new Array();
   
   //添加商品
    function addGoods() {

    var goodsName,price,gooodsId;
    var discount=$("#discount").val();
    var UseMoney=parseFloat(document.getElementById("sale").value);

        var Sindex = document.getElementById("sgoods").selectedIndex;
        var Sval = document.getElementById("sgoods").options[Sindex].value;
        var num=document.getElementById("num").value;

       if(checkNaN(document.getElementById("num")))
       {
        var  b= false;
         goodsName= document.getElementById("sgoods").options[Sindex].text;
              for(var i=0; i < x.length;i++)
              {
                 if(x[i]==goodsName)
                  {
                    b=true;
                    $("#showgoods tr").each(
                      function(){ 
                             var s =$(this).children("td").eq(0).text(); 
                             var s1=$(this).children("td").eq(1).text();
                             var s2=$(this).children("td").eq(2).text();
                              if(s==goodsName)
                              {
                                var shuliang=parseInt(s2); 
                                 var n=parseInt(num);
                                  shuliang += n;
                                 $(this).children("td").eq(2).text(shuliang);
                                  var Floats1=parseFloat(s1);
                                  var Floats2=parseFloat(n);
                                   UseMoney +=Floats1*Floats2;
                                   $("#sale").val(UseMoney.toFixed(2));
                              }
                            })
                  }
              }

  
         if(!b)
          {
           $.ajax({
            type:"POST",
            url:"<%:Url.Action("AddGoods","MemberSale") %>",
            data:"Sval="+Sval,
            cache:false,
            success:function(txt)
               {
                var str=   txt.toString().split('|');
               
              goodsName=str[0];
              price=str[1];
             gooodsId=str[2];
               x.push(goodsName);
               $("#showgoods").append("<tr><td>"+goodsName+"</td><td>"+price+"</td><td>"+num+"</td><td><input type=\"button\" value=\"删除\" class=\"button2\" onclick=\"delTr(this);\"></td></tr>");
                                  var Floats1=parseFloat(price);
                                  var Floats2=parseFloat(num);
                                     UseMoney+=Floats1*Floats2;
                                    $("#sale").val(UseMoney.toFixed(2));
               }
             })
          }
        }
    }

    //检查是否为数字
     function checkNaN(o) {
        if (!String.prototype.trim) {
            var TRIM_REG = /(^\s*)|(\s*$)/g;
            String.prototype.trim = function () { return this.replace(TRIM_REG, ''); }
        }
        var m = o.value.trim(); 
        
            reg = /^[0-9]*[1-9][0-9]*$/;
           
            if (!reg.test(m)) {
                alert("需要输入整数");
                return false;
            }
             return true;
    }

    //删除指定行
    function delTr(o)
    {
       
      var delPrice=parseFloat($(o).parent("td").prevAll("td").eq(1).text());
      var  delNum =parseFloat($(o).parent("td").prevAll("td").eq(0).text());
      var delgoods=$(o).parent("td").prevAll("td").eq(2).text();
      var allMoney=parseFloat($("#sale").val());
      var discountMoney= parseFloat($("#discountAmount").val());
      var left=allMoney - (delPrice*delNum);
      if(discountMoney>0){
      var discountMon=  parseFloat($("#discount").val())/10;
        var lessMoney=discountMoney-(delPrice*delNum*(discountMon));
         $("#discountAmount").val(lessMoney.toFixed(2));
      
      }
       for(var i=0 ;i<x.length;i++)
       {
          if(x[i]==delgoods)
          {
             x.splice(i,1);
          }
       }

       $("#sale").val(left.toFixed(2))
       $(o).parent("td").parent("tr").remove();
    }

    //改变卡片支付和现金支付金额
     function changeCardPay(){
       var sale=parseFloat($("#sale").val());
       var balance=parseFloat($("#balance").val());
       var cardpay=parseFloat($("#sale").val());
       
       if(cardpay<balance){
         $("#cardPay").val(sale.toFixed(2));
         $("#carhPay").val(0);
       }
       else{
       $("#cardPay").val(balance.toFixed(2));
       $("#carhPay").val((sale-parseFloat($("#cardPay").val())).toFixed(2));
       }
       
     }
     // 打折后金额
     function Afterdt(){ 
         var  disCount=document.getElementById("discount").value;
         if(disCount!=0 ){
           var sale=parseFloat($("#sale").val()).toFixed(2);
             if(sale>0){
               var Ammount=sale-(sale-(sale*disCount/10));
               $("#discountAmount").val(Ammount.toFixed(2));
               if(Ammount<=parseFloat($("#balance").val())){
                 $("#cardPay").val(Ammount.toFixed(2));
                 $("#carhPay").val(0);
               }
               else{
                  $("#cardPay").val($("#balance").val());
                  $("#carhPay").val((Ammount-parseFloat($("#cardPay").val())).toFixed(2));
               }
             }
         }
         else{
           $("#discountAmount").val($("#sale").val());
         }
     
     }

    //启用特殊折扣
    function tsDiscount(){

        if($("#Eable").val()=="开启"){
           
           $("#discount").removeAttr("disabled");
           $("#Eable").val("关闭");
        }else{
        $("#discount").attr("disabled", "true");
           $("#Eable").val("开启");
        }

    }

    //结算
      function MemberAccount(){
            var sData="";
            var memberId=$("#memberId").val();
            var discount=$("#discount").val();
            var sale=$("#sale").val();
            if(parseInt(sale)<=0){
              alert("未产生消费");
              return false;
            }
            var pwd=$("#pwd").val();
            var cardPay=$("#cardPay").val();
            var carhPay=$("#carhPay").val();
            var discountAmount=$("#discountAmount").val();
            var jiedairen=$("#jiedairen").val();
              var meiZu='';
                $("#showgoods tr").each(
                      function(){ 
                         
                          var s =$(this).children("td").eq(0).text();
                         if(s!="商品"){
                             var s =$(this).children("td").eq(0).text();
                             var s1=$(this).children("td").eq(1).text();
                             var s2=$(this).children("td").eq(2).text();
                               meiZu += s+','+s1+','+s2+'|';
                              }

                            });
             if(RequestStatus()==1){
                  var hComm = 0; 
                   hComm = GetCOM(); 
                   sData = vcp400.RfidCardReadSectorData(hComm, 0, 0, "", 15); 
                   vcp400.ocx_CommClose(hComm);
                 }
            if(parseInt(discount)>0 &&parseInt(discount)<10){
              if(confirm("本次消费："+sale+"\n 卡片支付："+cardPay+"\n 现金支付："+carhPay+"\n 折扣："+discount+"\n 打折后金额："+discountAmount+"")){
               $.ajax({
                      type: 'post',
                      url: '<%:Url.Action("MemberAccount","MemberSale") %>',
                      data: "memberId="+memberId+"&memberCardId="+sData+"&sale=" +sale+ "&pwd="+pwd+"&cardPay="+cardPay+"&carhPay="+carhPay+"&discount="+discount+"&discountAmount="+discountAmount+"&meiZu="+meiZu+"&jiedairen="+jiedairen+"",
                      cache: false,
                      success: function (html) {
                           if(html!="密码有误"&&html!="密码有误,请重新输入密码")
                        {
                       if(RequestStatus()==1){
                
                         PrintBufferClear();
                         SendText(html);
                         Print(); 
                          }
                              alert('消费成功');closeWindows();
                        }

                     else{
                       alert(html);
                       }
                     }

                   });
               } 
           }
           else{
           
            if(confirm("本次消费："+sale+"\n 卡片支付："+cardPay+"\n 现金支付："+carhPay+"")){ 
               $.ajax({
                      type: 'post',
                      url: '<%:Url.Action("MemberAccount","MemberSale") %>',
                      data: "memberId="+memberId+"&memberCardId="+sData+"&sale=" +sale+ "&pwd="+pwd+"&cardPay="+cardPay+"&carhPay="+carhPay+"&discount="+discount+"&discountAmount="+discountAmount+"&meiZu="+meiZu+"&jiedairen="+jiedairen+"",
                      cache: false,
                      success: function (html) {
                        if(html!="密码有误"&&html!="密码有误,请重新输入密码")
                        {
                       if(RequestStatus()==1){
                
                         PrintBufferClear();
                         SendText(html);
                         Print(); 
                          }
                              alert('消费成功');closeWindows();
                        }

                     else{
                       alert(html);
                       }
                     }
                     

                   });
                
               } 
           
           }             
      }


    //更换选项卡
    function setTab(name, cursel, n) {
        for (i = 1; i <= n; i++) {
            var menu = document.getElementById(name + i);
            var con = document.getElementById("con_" + name + "_" + i);
            menu.className = i == cursel ? "hover" : "";
            con.style.display = i == cursel ? "block" : "none";
        }
    }

      function GetQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return (r[2]); return null;
        }
     //关闭窗口
    function closeWindows() {
      var test=GetQueryString("memberId");
       if(test==null ||test=="" ){
         window.location.href="/Loading/Index/?src=/Desktop/Index/";
       }
       else{
       
        var i = parent.layer.getFrameIndex();
        parent.layer.close(i);
       }
    }
</script>

<%--套餐--%>
<script type="text/javascript">
    var memberId = $("#memberId").val();

    $.ajax({
        type: 'post',
        url: '<%:Url.Action("TaoCan","MemberSale") %>',
        data: "memberId=" + memberId + "",
        cache: false,
        success: function (html) {
            $("#WDH_goods").html(html);
        }

    });

    //兑换
    var TH_AllNum;
    var TH_goodsName;
    var TH_DanWei
    function DH(o) {
        TH_DanWei = $(o).parent("td").prevAll("td").eq(0).text();
        TH_AllNum = $(o).parent("td").prevAll("td").eq(1).text();
        TH_goodsName = $(o).parent("td").prevAll("td").eq(2).text();
        var str = '输入要兑换的数量：<input type="text" id="inputNum"><input type="button" value="确定" class="button2" onclick="getNum()">'
        var i = $.layer({
            type: 1,
            title: false,
            closeBtn: false,
            shade: [0.5, '#000'],
            shadeClose: false,
            area: ['230px', '40px'],
            shade: [0],
            page: {
                html: str

            },
            success: function () {
                layer.shift('left');
            }
        });
    }
    //要兑换的商品列表
    function getNum() {
        var inputNum = $("#inputNum").val();
        if (checkNaN(document.getElementById("inputNum"))) {
            if (parseInt(inputNum) <= parseInt(TH_AllNum)) {
                $("#DH_goods").append("<tr><td>" + TH_goodsName + "</td><td>" + inputNum + "</td><td>" + TH_DanWei + "</td></tr>");
                layer.closeAll();
            }
            else {
                alert("输入应小于数量");
            }

        }
        else {
            alert("请输入整数");
        }

    }
    // 清空打印缓存
    function PrintBufferClear() {
        var hComm = 0;

        hComm = GetCOM();

        vcp400.PrintBufferClear(hComm, 15);

        vcp400.ocx_CommClose(hComm);

    }
    function GetCOM() {
        COM = 100;
        Baud = 57600;

        return vcp400.ocx_commopen(COM, Baud);
    }
    // 发送文本到缓冲区
    function SendText(content) {

        var hComm = 0;
        var sData = "";
        var textx = 0;
        var texty = 0;

        hComm = GetCOM();
        //PrintBufferClear();
        stext = content;
        textx = 0;
        texty = 50;

        nrtn = vcp400.SendText(hComm, 1, textx, texty, stext, 15);
        // Print();
        vcp400.ocx_CommClose(hComm);

    }
      //读卡
      function RfidCardReadSectorData() {
       
        if(RequestStatus()==1){
        var hComm = 0;

        hComm = GetCOM();
         
        sData = vcp400.RfidCardReadSectorData(hComm, 0, 0, "", 15);
 
        vcp400.ocx_CommClose(hComm);
      
      }
    }
    //进行打印或擦除
    function Print() {
        var hComm = 0;

        hComm = GetCOM();

        vcp400.ErasePrintDischarge(hComm, 1, 1, 1, 15); 	// nMode=0, 不擦除，nMode=1 擦除 

        vcp400.ocx_CommClose(hComm);

    }
    // 退卡	
    function Dischange() {
        var hComm = 0;

        hComm = GetCOM();

        vcp400.Dischange(hComm, 1, 10); 	// 0=不完全退卡,1=完全退卡

        vcp400.ocx_CommClose(hComm);

    }
   
    // 获取机器状态	
    function RequestStatus() {
        var hComm = 0;
        hComm = GetCOM();
        sState = vcp400.RequestStatus(hComm, 1);
        if (sState.length = 6) {
            nchar = sState.charAt(0);
            switch (parseInt(nchar)) {
                case 1: return 1;
                    break;
            }
            nchar = sState.charAt(3);
            switch (parseInt(nchar)) {
                case 1: return 1; break;
            }
        }
        vcp400.ocx_CommClose(hComm);
    }	
</script>